<template>
    <el-container style="height: 100vh;">
        <el-header
            style="height: 60px; background-color: #FFF; display: flex; align-items: center; padding-left: 20px; border-bottom: 1px solid #ddd;">
            <div class="section-title">任务列表</div>
        </el-header>

        <el-main style="padding: 0;">
            <div class="table-container">
                <el-table :data="tableData" height="100%" style="width: 100%;" stripe border>
                    <el-table-column type="index" width="50" />
                    <el-table-column type="selection" width="60" />
                    <el-table-column label="任务名称" prop="taskNo" width="150" align="center" />
                    <el-table-column prop="method" label="表达方式" width="250" align="center" />
                    <el-table-column prop="describe" label="描述" width="350" align="center" />
                    <el-table-column prop="createdTime" label="创建时间" width="200" align="center" />
                    <el-table-column fixed="right" label="操作" min-width="250" align="center">
                        <template #default="scope">
                            <div style="display: flex; gap: 8px; justify-content: center;">
                                <el-button link type="primary" size="small" :loading="loadingTasks[scope.row.taskNo]"
                                    @click="handleClick(scope.row)">
                                    <span
                                        style="font-size: 18px; text-decoration: underline; text-decoration-color: #409EFF;">
                                        执行
                                    </span>
                                </el-button>
                                <!-- <el-button link type="warning" size="small" @click="handleSuspend(scope.row)">
                                    <span
                                        style="font-size: 18px; text-decoration: underline; text-decoration-color: #e6a23c;">
                                        暂停
                                    </span>
                                </el-button>
                                <el-button link type="info" size="small">
                                    <span
                                        style="font-size: 18px; text-decoration: underline; text-decoration-color: #909399;">
                                        日志
                                    </span>
                                </el-button> -->
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>
    </el-container>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

import {
    SyncWarehouseInfo,
    SyncUnitInfo,
    SyncInventoryClassInfo,
    SyncInventoryInfo,
    SyncDepartmentInfo,
    SyncEmployeesInfo,
    SyncDealingsUnitInfo,
    SyncPurchaseOrderInfo,
    GetChanjetToken,
    SyncSaleOrderInfo,
    SyncInventoryStockInfo,
} from '../../extension/V2/Sys_TaskTimed'

// 映射字符串到函数
const syncMap = {
    SyncWarehouseInfo,
    SyncUnitInfo,
    SyncInventoryClassInfo,
    SyncInventoryInfo,
    SyncDepartmentInfo,
    SyncEmployeesInfo,
    SyncDealingsUnitInfo,
    SyncPurchaseOrderInfo,
    SyncSaleOrderInfo,
    SyncInventoryStockInfo,
}

// 任务按钮 loading 状态对象
const loadingTasks = ref({})

// 任务列表，methodKey 字符串用于映射调用
const tableData = [
    {
        taskNo: '仓库同步',
        method: '0 0/1 * * * ?',
        describe: '同步仓库信息',
        createdTime: '2025-01-01',
        methodKey: 'SyncWarehouseInfo',
    },
    {
        taskNo: '单位同步',
        method: '0 0/1 * * * ?',
        describe: '同步单位信息',
        createdTime: '2025-02-02',
        methodKey: 'SyncUnitInfo',
    },
    {
        taskNo: '存货分类同步',
        method: '0 0/1 * * * ?',
        describe: '同步存货分类',
        createdTime: '2025-03-03',
        methodKey: 'SyncInventoryClassInfo',
    },
    {
        taskNo: '存货信息同步',
        method: '0 0/1 * * * ?',
        describe: '同步存货信息',
        createdTime: '2025-03-03',
        methodKey: 'SyncInventoryInfo',
    },
    {
        taskNo: '部门信息同步',
        method: '0 0/1 * * * ?',
        describe: '同步部门信息',
        createdTime: '2025-03-03',
        methodKey: 'SyncDepartmentInfo',
    },
    {
        taskNo: '员工信息同步',
        method: '0 0/1 * * * ?',
        describe: '同步员工信息',
        createdTime: '2025-03-03',
        methodKey: 'SyncEmployeesInfo',
    },
    {
        taskNo: '往来单位同步',
        method: '0 0/1 * * * ?',
        describe: '同步往来单位',
        createdTime: '2025-03-03',
        methodKey: 'SyncDealingsUnitInfo',
    },
    {
        taskNo: '采购订单同步',
        method: '0 0/1 * * * ?',
        describe: '同步采购订单',
        createdTime: '2025-03-03',
        methodKey: 'SyncPurchaseOrderInfo',
    },
    {
        taskNo: '存货库存同步',
        method: '0 0/1 * * * ?',
        describe: '同步存货库存',
        createdTime: '2025-03-03',
        methodKey: 'SyncInventoryStockInfo',
    },
    {
        taskNo: '销售订单同步',
        method: '0 0/1 * * * ?',
        describe: '同步销售订单',
        createdTime: '2025-03-03',
        methodKey: 'SyncSaleOrderInfo',
    },
]

const handleClick = async (row) => {
    // await GetChanjetToken()
    const name = row.taskNo
    const fn = syncMap[row.methodKey]
    if (!fn) {
        ElMessage.error(`"${name}" 对应的同步方法未找到`)
        return
    }
    if (loadingTasks.value[name]) return

    try {
        loadingTasks.value[name] = true
        ElMessage.info(`开始执行：${name}`)
        await fn()
        ElMessage.success(`${name} 执行成功`)
    } catch (e) {
        console.error(e)
        ElMessage.error(`${name} 执行失败: ${(e && e.message) ? e.message : '未知错误'}`);
    } finally {
        loadingTasks.value[name] = false
    }
}

const handleSuspend = (row) => {
    console.log(`暂停任务：${row.taskNo}`)
    ElMessage.warning(`${row.taskNo} 已暂停`)
}
</script>



<style scoped>
.table-container {
    padding: 20px;
    height: calc(100vh - 60px);
    /* 扣除 header 高度 */
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

.section-title {
    font-size: 18px;
    font-weight: bold;
    color: #333;
    margin-bottom: 10px;
    margin-top: 30px;
    border-bottom: 2px solid #333;
    display: inline-block;
}
</style>
